package com.javafactroy.kunlong.codefactroy;

import com.google.common.collect.BiMap;
import com.javafactroy.kunlong.DbAgent;
import com.javafactroy.kunlong.base.metadata.model.TMetadataModel;
import com.javafactroy.kunlong.base.metadata.valueobj.MetadataTable;
import com.javafactroy.kunlong.codefactroy.service.tool.DbCodeMysqlTool;
import com.javafactroy.kunlong.data.entity.AttDaysApiEntity;
import com.javafactroy.kunlong.codefactroy.service.base.FileTemplateFactroy;
import com.javafactroy.kunlong.codefactroy.service.domain.valueobj.EntityProps;
import com.javafactroy.kunlong.data.valueobj.GeneralFieldRefModel;
import com.javafactroy.kunlong.common.utils.BeanFactoryUtils;
import com.javafactroy.kunlong.common.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.lang.reflect.Field;

@SpringBootTest(classes = {DbAgent.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ExtendWith(SpringExtension.class)
@Slf4j
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@TestMethodOrder(MethodOrderer.MethodName.class)
public class TestCodeFactroyMysql {

    @Autowired
    GeneralFieldRefModel generalFieldRefModel;

    @Autowired
    BeanFactoryUtils beanFactoryUtils;

    @Autowired
    TMetadataModel metadataModel;

    @Autowired
    FileTemplateFactroy fileTemplateFactroy;

    @Autowired
    DbCodeMysqlTool dbCodeMysqlTool;

    @Test
    public void test001_makeMysql() {
        dbCodeMysqlTool.makeMysql("employee");
    }

    @Test
    public void test002_makeMysqlAll() {
        dbCodeMysqlTool.makeMysqls();
    }

    @Test
    public void test003_makeMysql() {
        dbCodeMysqlTool.makeMysql("v_dept");
    }

    @Test
    public void test004_makeMysqlView() {
        dbCodeMysqlTool.makeMysql("v_dptEmp");
    }

    @Test
    public void test005_makeMysqlLike() {
        // dbCodetool.makeMysql("t_sys_user"); dbCodetool.makeMysql("coupon");
        dbCodeMysqlTool.makeMysqlLike("query_");
    }

    @Test
    public void test006_makeMysqlLike() {

        dbCodeMysqlTool.makeMysqlLike("sys_app");

    }

    //TentityProps
    @Test
    public void test007_makeProps() {

        MetadataTable tmetadataTable = metadataModel.loadDbMysqlMetadata("tasklog");

        EntityProps entityProps = tmetadataTable.toEntityProps();
        entityProps.makeParams();
        log.info("template params={}", CommonUtils.toJSONString(entityProps.getStringTemplate().getParams()));

        fileTemplateFactroy.rmDbFile(tmetadataTable);

        fileTemplateFactroy.getList().forEach(i -> {

            String stext = i.valueOf(entityProps).parse(i.readFile());
            i.writeFile(stext);
        });
    }

    @Test
    public void test008_refModel() {

        Field[] fields = generalFieldRefModel.refProps(AttDaysApiEntity.class);
        BiMap<String, String> map = generalFieldRefModel.refProp2FieldsMap(AttDaysApiEntity.class);
        log.info("fields = {}", CommonUtils.toJSONString(map));
    }

    @Test
    public void test009_beanFactry() {
        GeneralFieldRefModel refModel = beanFactoryUtils.getBean(GeneralFieldRefModel.class);
        log.info("{}, {}, \r\nisSingle={}", generalFieldRefModel, refModel, beanFactoryUtils.isSingleton("beanFactoryUtils"));

    }

}
